<template>
  <div class="home flex h100">
    <el-container class="w100">
      <el-aside style="height: 100%" class="home-left h100">
        <h5 class="flex">
          <div class="textCen"><i class="el-icon-dish"></i></div>
          外卖商家中心
        </h5>
        <el-menu
          :default-active="menuact"
          class="el-menu-vertical-demo"
          unique-opened
          background-color="#706c61"
          text-color="#fff"
          active-text-color="#ff8882"
          router
        >
          <div v-for="item in rolelist" :key="item.index">
            <el-submenu :index="item.index" v-if="item.children">
              <template slot="title">
                <i :class="item.icon"></i>
                <span>{{ item.title }}</span>
              </template>
              <el-menu-item
                v-for="child in item.children"
                :key="child.index"
                :index="child.index"
                >{{ child.title }}</el-menu-item
              >
            </el-submenu>
            <el-menu-item :index="item.index" v-else>
              <i :class="item.icon"></i>
              <span slot="title">{{ item.title }}</span>
            </el-menu-item>
          </div>
        </el-menu>
      </el-aside>
      <el-main class="home-right" style="padding: 0 0 50px 0">
        <div class="home-right-title flex" @click="adminFn">
          <span> {{ header }} </span>
          <div class="img">
            <img :src="imgUrl" alt="" />
          </div>
        </div>
        <router-view></router-view>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import { checktoken, accountinfo } from "../apis/user";

export default {
  data() {
    return {
      menuact: "/home/homemain",
      treelist: [
        {
          index: "/home/homemain",
          icon: "el-icon-s-home",
          title: "后台首页",
          role: ["super", "normal"],
        },
        {
          index: "/home/ordermanange",
          icon: "el-icon-s-order",
          title: "订单管理",
          role: ["super", "normal"],
        },
        {
          index: "3",
          icon: "el-icon-goods",
          title: "商品管理",
          role: ["super", "normal"],

          children: [
            { index: "/home/prolist", title: "商品列表" },
            { index: "/home/addpro", title: "商品添加" },
            { index: "/home/proclass", title: "商品分类" },
          ],
        },
        {
          index: "/home/storemanange",
          icon: "el-icon-s-shop",
          title: "店铺管理",
          role: ["super"],
        },
        {
          index: "5",
          icon: "el-icon-user",
          title: "账号管理",
          role: ["super"],
          children: [
            { index: "/home/accoutlist", title: "账号列表" },
            { index: "/home/addaccout", title: "添加账号" },
            { index: "/home/editaccout", title: "修改密码" },
          ],
        },
        {
          index: "6",
          icon: "el-icon-s-data",
          title: "销售统计",
          role: ["super"],
          children: [
            { index: "/home/prostatistics", title: "商品统计" },
            { index: "/home/orderstatistics", title: "订单统计" },
          ],
        },
      ],
      header: "",
      imgUrl: "",
    };
  },
  created() {
    this.menuact = this.$route.name;
    checktoken({ token: localStorage.token }).then((res) => {
      if (res.data.code == 0) {
        accountinfo({ id: localStorage.id }).then((res) => {
          this.header = res.data.accountInfo.account;
          this.imgUrl = res.data.accountInfo.imgUrl;
        });
      } else {
        this.header = "请登录";
        this.imgUrl = "";
      }
    });
    this.$bus.on("avatarUploadSuccess", (url) => {
      this.imgUrl =
        this.imgUrl.substring(0, this.imgUrl.lastIndexOf("/") + 1) + url;
    });
  },
  computed: {
    rolelist() {
      return this.treelist.filter((obj) =>
        obj.role.includes(localStorage.role)
      );
    },
  },
  methods: {
    adminFn() {
      if (this.header == "请登录") {
        this.$router.push("/");
      } else {
        this.$router.push("/home/admin");
      }
    },
  },
};
</script>
<style lang="less" scoped>
a {
  color: #fff;
}
.el-aside {
  background-color: #706c61;
  width: 200px;
}
.el-menu {
  border: 0 solid #fff;
}
h5 {
  line-height: 50px;
  color: #fff;
  padding: 20px 0 14px 14px;
  border-bottom: 4px solid #fff;
  div {
    width: 50px;
    height: 50px;
    border-radius: 25px;
    background-color: #e1f4f3;
    margin-right: 10px;
    i {
      line-height: 50px;
      font-size: 30px;
      color: #333;
    }
  }
}
.home-right {
  background-color: #f6f5f5;
  position: relative;
  .home-right-title {
    line-height: 88px;
    position: absolute;
    right: 20px;
    top: 0;
    .img {
      width: 70px;
      height: 70px;
      border-radius: 35px;
      border: 1px solid #333;
      text-align: center;
      margin-top: 9px;
      margin-left: 10px;
      overflow: hidden;
      img {
        height: 100%;
        width: 100%;
      }
    }
  }
}
</style>
